package top.zywork.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zywork.common.BeanUtils;
import top.zywork.dao.UserGoodsOrderDAO;
import top.zywork.dao.UserGoodsOrderItemDAO;
import top.zywork.dos.UserGoodsOrderDO;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.UserGoodsOrderDTO;
import top.zywork.query.UserGoodsOrderQuery;
import top.zywork.service.AbstractBaseService;
import top.zywork.service.UserGoodsOrderService;
import top.zywork.vo.UserGoodsOrderExVO;
import top.zywork.vo.UserGoodsOrderItemVO;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * UserGoodsOrderServiceImpl服务接口实现类<br/>
 *
 * 创建于2019-07-04<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@Service(value = "userGoodsOrderService")
public class UserGoodsOrderServiceImpl extends AbstractBaseService implements UserGoodsOrderService {

    private UserGoodsOrderDAO userGoodsOrderDAO;

    private UserGoodsOrderItemDAO userGoodsOrderItemDAO;

    @Autowired
    public void setUserGoodsOrderDAO(UserGoodsOrderDAO userGoodsOrderDAO) {
        super.setBaseDAO(userGoodsOrderDAO);
        this.userGoodsOrderDAO = userGoodsOrderDAO;
    }

    @Autowired
    public void setUserGoodsOrderItemDAO(UserGoodsOrderItemDAO userGoodsOrderItemDAO) {
        this.userGoodsOrderItemDAO = userGoodsOrderItemDAO;
    }

    @PostConstruct
    public void init() {
        super.init(UserGoodsOrderDO.class, UserGoodsOrderDTO.class);
    }

    @Override
    public PagerDTO listPageExByCondition(UserGoodsOrderQuery userGoodsOrderQuery) {
        PagerDTO pagerDTO = new PagerDTO();
        Long count = userGoodsOrderDAO.countByCondition(userGoodsOrderQuery);
        if (count <= 0) {
            pagerDTO.setTotal(0L);
            pagerDTO.setRows(new ArrayList<>());
            return pagerDTO;
        }
        List<UserGoodsOrderDO> userGoodsOrderDOList = userGoodsOrderDAO.listPageIdByCondition(userGoodsOrderQuery);
        StringBuilder goodsOrderIds = new StringBuilder();
        // 取出所有的订单id
        for (UserGoodsOrderDO userGoodsOrderDO : userGoodsOrderDOList) {
            if (StringUtils.isEmpty(goodsOrderIds.toString())) {
                goodsOrderIds.append(userGoodsOrderDO.getGoodsOrderId());
            } else {
                goodsOrderIds.append(",").append(userGoodsOrderDO.getGoodsOrderId());
            }
        }
        String[] userGoodsOrderIdsArr = goodsOrderIds.toString().split(",");
        // 根据这些订单id取出所有的订单项
        List<Object> objectList = userGoodsOrderItemDAO.listByIds(userGoodsOrderIdsArr);
        // 将数据封装成一个扩展的VO对象
        List<Object> userGoodsOrderExVOList = new ArrayList<>();
        List<UserGoodsOrderItemVO> userGoodsOrderItemVOList = BeanUtils.copy(objectList, UserGoodsOrderItemVO.class);
        for (UserGoodsOrderDO userGoodsOrderDO : userGoodsOrderDOList) {
            Long orderId = userGoodsOrderDO.getGoodsOrderId();
            UserGoodsOrderExVO userGoodsOrderExVO = BeanUtils.copy(userGoodsOrderDO, UserGoodsOrderExVO.class);
            List<UserGoodsOrderItemVO> tempUserGoodsOrderItemVO = new ArrayList<>();
             for (UserGoodsOrderItemVO userGoodsOrderItemVO : userGoodsOrderItemVOList) {
                 if (orderId.equals(userGoodsOrderItemVO.getGoodsOrderId())) {
                     // 当前的订单项是同一个订单的
                     tempUserGoodsOrderItemVO.add(userGoodsOrderItemVO);
                 }
             }
            userGoodsOrderExVO.setUserGoodsOrderItemVOList(tempUserGoodsOrderItemVO);
            userGoodsOrderExVOList.add(userGoodsOrderExVO);
         }
        pagerDTO.setRows(userGoodsOrderExVOList);
        pagerDTO.setTotal(count);
        return pagerDTO;
    }
}
